<?php
namespace app\index\controller;
header("Access-Control-Allow-Origin: *");
header("Access-Control-Allow-Methods:POST,GET");

use app\common\controller\HomeBase;

use think\Db;

class Cardetc extends HomeBase
{
	protected function _initialize(){
		parent::_initialize();
	}

	/**
	*公共方法:将数组进行判断
	*@param  $arr		  要判断的数组
	**/
	public function doif($arr)
	{
		if($arr){
            $this->apiSuccess($arr);
        }else{
            $this->apiError('没有查到信息');
        }
	}

	/**
	*油卡ETC首页显示
	*@param  int id 		  油卡ETC的id
	**/
	public function index()
	{
		$id = $_GET['id'];
		$operations = Db('nav')->where('pid',$id)->select();
		$this->doif($operations);
	}

	/**
	*油卡ETC充值页面显示
	*@param  int nav_id 		  油卡ETC下办理业务的id
	*@param  int uid		  	  用户id
	**/
	public function rechargeShow()
	{
		//取得对应的业务下面用户的卡
		$uid = $_GET['uid'];
		$nav_id = $_GET['nav_id'];
		$cards = Db('kapian')->where('nav_id',$nav_id)->where('user_id',$uid)->select();		
		$list['cards'] = $cards;
		//获得对应业务的充值信息
		$recharge = Db('recharge')->where('status',1)->select();
		$i = 0;
		for(;$i < count($recharge);$i++){
			$recharge[$i]['discount'] = ($recharge[$i]['discount']/10);
		}
		$list['recharge'] = $recharge;
		$this->doif($list);
	}

	/**
	*油卡的绑定/添加
	*@param  int uid		  	  用户id
	**/
	public function CarAdd()
	{
		//具体的卡
		$nav_id = $_POST['nav_id'];
		//获取页面穿过来的信息
		$uid = $_POST['uid'];
		$card_number = htmlspecialchars($_POST['card_number']);
		$id_number = htmlspecialchars($_POST['id_number']);
		$phone = htmlspecialchars($_POST['phone']);
		$name = htmlspecialchars($_POST['name']);

		//判断该用户是否已经绑定过该卡
		$card = Db('kapian')->where('user_id',$uid)->where('nav_id',$nav_id)->column('card_number');
		if(in_array($card_number, $card)){
			$this->apiError('该卡已添加');
		}else{
			//添加数据库
			$data = [
				'user_id' => $uid,
				'nav_id' => $nav_id,
				'name' => $name,
				'phone' => $phone,
				'card_number' => $card_number,
				'id_number' => $id_number,
			];
			$add = Db('kapian')->insert($data);
			if($add){
				$this->apiSuccess('','添加成功');
			}else{
				$this->apiError('添加失败,请重试');
			}
		}
	}

	/**
	*申请办理 办理须知页面显示(显示用户办理需要的材料)
	*@param  int nav_id 		  证件办理的具体业务的nav_id
	**/
	public function creditCard()
	{
		$nav_id = $_GET['nav_id'];
		$list = Db('nav')->where('id',$nav_id)->select();
		dump($list);
		$this->doif($list);
	}

	/**
	*申请办理添加预留信息
	*@param  int nav_id   证件办理的具体业务的nav_id
	*@param  int id 	  油卡ETC的id
	**/
	public function reserveadd()
	{
		$classifyID = $_POST['id'];
		$nav_id = $_POST['nav_id'];
		$name = htmlspecialchars($_POST['name']);
		$phone = htmlspecialchars($_POST['phone']);

		//判断该手机号是否注册过
		$number = Db('reserved')->where('nav_id',$nav_id)->where('reservedPhone',$phone)->find();
		if ($number) {
			$this->apiError('您的手机已申请,请耐心等待');
			die;
		}

		// 获取表单上传文件
		$files = request()->file('file');
		if ($files) {
			foreach($files as $file){
				// 移动到框架应用根目录/public/uploads/ 目录下
				$info = $file->move(ROOT_PATH . 'public' . DS . 'uploads');
				if($info){
					// 成功上传后 将成功的文件名存入数组
					$arr[] = $info->getFilename();
					
				}else{
					// 上传失败获取错误信息
					echo $file->getError('图片添加失败,请重试');
				}
			}
			//遍历拼接数组名字
			foreach($arr as $photoName){
				$data = date('Ymd',strtotime('now'));
				$path = '/uploads/'.$data.'/'.$photoName;
				$photo[] = $path;
			}

			//将数组序列化之后存入数据库
			$photo = serialize($photo);
		}else{
			$photo = '';
		}
		$reservedDate = date('Y-m-d H:i:s',strtotime('now'));
		$data = [
			'classifyID'=>$classifyID,
			'nav_id'=>$nav_id,
			'reservedName'=>$name,
			'reservedPhone'=>$phone,
			'reservedDate'=>$reservedDate,
			'photo'=>$photo
		];
		$add = Db('reserved')->insert($data);
		if($add){
			//添加成功之后返回公司的电话号码
			$phoneList = Db('system')->select();
			$value = unserialize($phoneList[0]['value']);
			$arr[] = $value['site_phone'];
			$this->apiSuccess($arr,'添加成功');
		}else{
			$this->apiError('添加失败,请重试');
		}
	}

}